//
//  MQProductViewController.m
//  meiquan
//
//  Created by hejingjin on 2017/11/2.
//  Copyright © 2017年 mq. All rights reserved.
//

#import "MQProductViewController.h"
#import "MQProducInfoTopview.h"
#import "MQBaseUserInfoModel.h"
#import "MQgoumaiview.h"
#import "MQSendProductViewController.h"
#import <UShareUI/UShareUI.h>
#import "MQadviews.h"
#import "MQAdmodels.h"
@interface MQProductViewController ()<UIWebViewDelegate>

@property(nonatomic,strong)MQProducInfoTopview *infoview;

@property(nonatomic,strong)UIWebView *productdetailinfowebView;

@property(nonatomic,strong)MQProductListModel *currentmodel;
@property(nonatomic,strong)UIButton *goumaibutton;

@property(nonatomic,strong)MQgoumaiview *goumaiview;

@property(nonatomic,strong)MQadviews *adviews;

@property(nonatomic,strong)MQAdmodels *admodels;

@end

@implementation MQProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"商品详情";
    [self setUpMainInterface];
    
    [self getproductFromSever];
    
    [self getaddata];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

-(void)getaddata{
    
    NSArray *array = @[@"page1",@"@page2",@"page3"];
    int x = arc4random() % 3;
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:userinfo.userId forKey:@"userId"];
    [param setValue:array[x] forKey:@"code"];
    [param setValue:@"page1" forKey:@"code"];
    
    [MQRequestTools POSTWithMethod:adv_index params:param success:^(id json) {
        
        NSDictionary *dic = [json valueForKey:@"data"];
        _admodels = [MQAdmodels mj_objectWithKeyValues:dic];
        if (_admodels.advId) {
            [_adviews.adimageviews sd_setImageWithURL:[NSURL URLWithString:_admodels.pic] placeholderImage:[UIImage imageNamed:@""]];
        }else{
            _adviews.bst_height = 0;
            _productdetailinfowebView.bst_y = NAVHEIGHT;
            
        }
        
        
        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [_adviews addGestureRecognizer:tapGesturRecognizer];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

-(void)tapAction:(UITapGestureRecognizer *)obj{
    NSLog(@"%@",_admodels.url);
    MQAdDetailViewController *advc = [[MQAdDetailViewController alloc] init];
    advc.url = _admodels.url;
     advc.type = _admodels.type;
    [self.navigationController pushViewController:advc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


-(void)getproductFromSever{
    
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:userinfo.userId forKey:@"userId"];
    [param setValue:_model.productId forKey:@"productId"];
    
    [MQRequestTools POSTWithMethod:product_detail params:param success:^(id json) {
        
        _currentmodel = [MQProductListModel mj_objectWithKeyValues:[json valueForKey:@"data"]];
        
        
        _infoview.circleview.imageDataarray = [_currentmodel.listPic mutableCopy];
         _infoview.producnameLabel.text = _currentmodel.name;
        _goumaiview.nameLabel.text = _currentmodel.name;
        _goumaiview.kuchunlabel.text = [NSString stringWithFormat:@"库存: %ld",_currentmodel.stocks];
        _infoview.kuncunlabel.text = [NSString stringWithFormat:@"库存: %ld",_currentmodel.stocks];
        [_goumaiview.imageview sd_setImageWithURL:[NSURL URLWithString:_currentmodel.pic] placeholderImage:[UIImage imageNamed:@""]];
        _infoview.xiaoliangLabel.text = [NSString stringWithFormat:@"销量 :  %ld",_currentmodel.sales];
        if (_model.agentPrice) {
            _goumaiview.priceLabel.text = [NSString stringWithFormat:@"¥ %@",_model.agentPrice];
            _infoview.pricelabel.text = [NSString stringWithFormat:@"¥ %@",_model.agentPrice];
        }else{
            _goumaiview.priceLabel.text = [NSString stringWithFormat:@"¥ %@",_model.price];
            _infoview.pricelabel.text = [NSString stringWithFormat:@"¥ %@",_model.price];
        }
//
        [_infoview layoutIfNeeded];
        
        CGFloat height = MAX_Y(_infoview.detaliLabel.frame);
        _infoview.bst_height = height;
        _infoview.bst_y = -height;
        _productdetailinfowebView.scrollView.contentInset = UIEdgeInsetsMake(height, 0, 0, 0);
         [_productdetailinfowebView loadHTMLString:_currentmodel.details baseURL:nil];
        self.navigationItem.rightBarButtonItem.enabled = YES;
    } failure:^(NSError *error) {
        
    }];
    
    
    
    
}

-(void)setUpMainInterface{
    
    
    self.view.backgroundColor = BSTColorFromRGB(0xf3f3f3);
    
    _adviews = [MQadviews loadadviews];
    _adviews.bst_x = 0;
    _adviews.bst_width = BSTScreenW;
    _adviews.bst_height = 70 *Persion;
    _adviews.bst_y = NAVHEIGHT;
    [self.view addSubview:_adviews];
    
    _infoview = [MQProducInfoTopview loadProductinfoTopview];

    
    _infoview.circleview.imageDataarray = [@[@"banner",@"banner"] mutableCopy];
    
    CGFloat height = MAX_Y(_infoview.detaliLabel.frame);
    _infoview.bst_height = height;
    _infoview.bst_x = 0;
    _infoview.bst_y = -height;
    _infoview.bst_width = BSTScreenW;
   
    [self.view addSubview:_infoview];
    
    
    _productdetailinfowebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, NAVHEIGHT + 70 * Persion, BSTScreenW , BSTScreenH - NAVHEIGHT - TABBARHEIGHT - 70 * Persion)];
    _productdetailinfowebView.scrollView.contentInset = UIEdgeInsetsMake(height, 0, 0, 0);
    _productdetailinfowebView.backgroundColor = [UIColor whiteColor];
    _productdetailinfowebView.opaque = NO;
    _productdetailinfowebView.delegate = self;
    _productdetailinfowebView.hidden = YES;
    [_productdetailinfowebView.scrollView addSubview:_infoview];
    [self.view addSubview:_productdetailinfowebView];
    
    
    UIButton *goumaibutton = [[UIButton alloc] init];
    goumaibutton.bst_x = 0;
    goumaibutton.bst_y = BSTScreenH - TABBARHEIGHT;
    goumaibutton.bst_width = BSTScreenW;
    goumaibutton.bst_height = TABBARHEIGHT;
    
    goumaibutton.titleLabel.font = [UIFont systemFontOfSize:15];
    [goumaibutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [goumaibutton setBackgroundColor:BSTColorFromRGB(0xff8e01)];
    [goumaibutton setTitle:@"立即购买" forState:UIControlStateNormal];
    [goumaibutton addTarget:self action:@selector(goumaibuttonpressed:) forControlEvents:UIControlEventTouchUpInside];
    
    goumaibutton.hidden = YES;
    _goumaibutton = goumaibutton;
    [self.view addSubview:goumaibutton];
    
    
    MQgoumaiview *goumaiview = [MQgoumaiview loadgoumaiviews];
    
    goumaiview.bst_x = 0;
    goumaiview.bst_y = 0;
    goumaiview.bst_width = BSTScreenW;
    goumaiview.bst_height = BSTScreenH;
    
    goumaiview.addbutton.layer.borderWidth = 0.5f;
    goumaiview.jianbutton.layer.borderWidth = .5f;
    goumaiview.coutlabel.layer.borderWidth = .5f;
    
    goumaiview.addbutton.layer.borderColor = BSTColorFromRGB(0xebebeb).CGColor;
    goumaiview.jianbutton.layer.borderColor = BSTColorFromRGB(0xebebeb).CGColor;
    goumaiview.coutlabel.layer.borderColor = BSTColorFromRGB(0xebebeb).CGColor;
    goumaiview.image.layer.borderColor = BSTColorFromRGB(0xebebeb).CGColor;
    goumaiview.image.layer.borderWidth = .5f;
    goumaiview.image.layer.cornerRadius = 8.f;
    goumaiview.image.layer.masksToBounds = YES;
    [goumaiview.addbutton addTarget:self action:@selector(adbuttonpressed:) forControlEvents:UIControlEventTouchUpInside];
    [goumaiview.jianbutton addTarget:self action:@selector(jianbuttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [goumaiview.colsebutton addTarget:self action:@selector(closebuttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [goumaiview.buybutton addTarget:self action:@selector(buybuttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    goumaiview.addbutton.layer.borderColor = BSTColorFromRGB(0xebebeb).CGColor;
    
    goumaiview.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
    _goumaiview = goumaiview;
    
   
    UIButton *btn = [[UIButton alloc] initWithFrame:(CGRectMake(0, 0, 40, 40))];
    [btn setTitle:@"分享" forState:(UIControlStateNormal)];
    [btn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [btn addTarget:self action:@selector(sharePress) forControlEvents:(UIControlEventTouchUpInside)];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    rightItem.enabled = NO;
    self.navigationItem.rightBarButtonItem = rightItem;

}

-(void)sharePress{
    //显示分享面板
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
    
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        id data = [NSData dataWithContentsOfURL:[NSURL URLWithString: _currentmodel.listPic[0]]];
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:_infoview.producnameLabel.text descr:nil thumImage:data];
        shareObject.webpageUrl = _currentmodel.listPic[0];
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
            
        }];
    }];

}

-(void)goumaibuttonpressed:(UIButton *)sender{
    AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [appDelegate.window addSubview:_goumaiview];
}

-(void)adbuttonpressed:(UIButton *)sender{
    _goumaiview.coutlabel.text = [NSString stringWithFormat:@"%ld",[_goumaiview.coutlabel.text integerValue] + 1];
}

-(void)jianbuttonPressed:(UIButton *)sender{
    NSInteger index = [_goumaiview.coutlabel.text integerValue];
    if (index >1) {
        _goumaiview.coutlabel.text = [NSString stringWithFormat:@"%ld",[_goumaiview.coutlabel.text integerValue]  - 1];
    }

}

-(void)buybuttonPressed:(UIButton *)sender{
    
    MQSendProductViewController *sendproductvc = [[MQSendProductViewController alloc] init];
    sendproductvc.model = _currentmodel;
    sendproductvc.currentcount = [_goumaiview.coutlabel.text integerValue];
    [self.navigationController pushViewController:sendproductvc animated:YES];
    
    [_goumaiview removeFromSuperview];

    
}

-(void)closebuttonPressed:(UIButton *)sender{
    [_goumaiview removeFromSuperview];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    _goumaibutton.hidden = NO;
    _productdetailinfowebView.hidden = NO;
    
}

@end
